package cn.kooki.app.duobao.ui.view;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.util.ScreenUtils;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;


/**
 * Created by xiong on 2015/12/14.
 */
public class CustomRefreshHeader extends RelativeLayout implements PtrUIHandler {

    private static final String TAG = "CustomRefreshHeader";
    @Bind(R.id.refresh_progress)
    ProgressBar refreshProgress;
    @Bind(R.id.refresh_img)
    ImageView refreshImg;
    @Bind(R.id.refresh_text)
    TextView refreshText;
    @Bind(R.id.rl_loading)
    RelativeLayout rlLoading;

    private int drableheight;
    private int drablewidth;
    private int leftx;
    private int rightx;
    private int screenWidth;
    private int currtdrablecentery;
    private Paint paint;

    public CustomRefreshHeader(Context context) {
        super(context);
        init();
    }

    private void init() {
        Context context = getContext();
        LayoutInflater.from(context).inflate(R.layout.refresh_header_layout, this, true);
        ButterKnife.bind(this);
//        screenWidth = ScreenUtils.getScreenWidth(getContext());

    }


    private void resetView() {
        refreshProgress.setVisibility(INVISIBLE);
        refreshText.setVisibility(INVISIBLE);
        refreshImg.setScaleX(0);
        refreshImg.setScaleY(0);
    }

    @Override
    public void onUIReset(PtrFrameLayout frame) {
        resetView();
    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {

    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        refreshProgress.setVisibility(VISIBLE);
        refreshText.setVisibility(VISIBLE);
        refreshImg.setVisibility(INVISIBLE);
        refreshText.setText(R.string.load_more_layout_text);
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        refreshText.setText("加载完成");
        refreshProgress.setVisibility(INVISIBLE);
        refreshImg.setVisibility(VISIBLE);

    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
        final int mOffsetToRefresh = frame.getOffsetToRefresh();
        final int currentPos = ptrIndicator.getCurrentPosY();
        final int lastPos = ptrIndicator.getLastPosY();
        if (currentPos<=mOffsetToRefresh){
            float v = currentPos * 1.0f / mOffsetToRefresh;
            refreshImg.setScaleX(v);
            refreshImg.setScaleY(v);
        }
    }


}
